package com.bdqn.boot.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Hasee
 * @version 1.0
 * @className LoginInterceptor
 * @description TODD
 * @date 2025/2/25 14:10
 */
@Slf4j
public class LoginInterceptor  implements HandlerInterceptor {
    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求路径
        String uri = request.getRequestURI();
        log.info("请求的路径是:{}",uri);
        //判断是否登录
        Object userName = request.getSession().getAttribute("userName");
        if (userName == null) {
            //不为空，登录成功
            return true;//成功
        }
        return false;//默认不成功
    }

    //目标方法执行完成以后

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("目标方法执行前{}",modelAndView);
    }

    //页面渲染以后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("数据渲染执行后{}",ex);
    }

}
